package cn.jbolt.admin.monitor;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.httpmethod.JBoltHttpGet;
import cn.jbolt.core.base.JBoltMsg;
import cn.jbolt.core.common.enums.JBoltUserOnlineState;
import cn.jbolt.core.enumutil.JBoltEnum;
import cn.jbolt.core.model.LoginLog;
import cn.jbolt.core.model.base.JBoltBaseModel;
import cn.jbolt.core.para.JBoltNoUrlPara;
import cn.jbolt.core.permission.CheckPermission;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import cn.jbolt.core.service.JBoltLoginLogService;
import cn.jbolt.core.service.JBoltOnlineUserService;
import cn.jbolt.core.service.base.JBoltBaseService;
import cn.jbolt.extend.permission.PermissionKey;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;

@CheckPermission(PermissionKey.ONLINE_USER)
@UnCheckIfSystemAdmin
@Path("/api/admin/onlineUser")
public class OnlineUserAdminController extends JBoltApiBaseController {
    @Inject
    JBoltLoginLogService loginLogService;
    @Inject
    JBoltOnlineUserService service;

    @Override
    protected JBoltBaseService<? extends JBoltBaseModel<?>> getModelService() {
        return service;
    }

    /**
     * 查看详情
     */
    @JBoltHttpGet
    public void index() {
        renderJBoltApiRet(findModelById());
    }

    /**
     * 分页条件查询接口
     * @param page
     * @param pageSize
     * @param state
     * @param keywords
     */
    @JBoltHttpGet
    public void datas(@Para(value = PAGE_PARAM_NAME,defaultValue = "1")Integer page,
                      @Para(value = PAGESIZE_PARAM_NAME,defaultValue = "15")Integer pageSize,
                      @Para("state")Integer state,
                      @Para("keywords")String keywords){
        renderJBoltApiSuccessWithData(service.paginateAdminDatas(page,pageSize,keywords,state));

    }

    /**
     * 查看登录日志详情
     */
    @JBoltHttpGet
    public void loginLog(){
        Long loginLogId = getLong(0);
        if(notOk(loginLogId)){
            renderJBoltApiFail(JBoltMsg.PARAM_ERROR);
            return;
        }
        LoginLog loginLog =  loginLogService.findById(loginLogId);
        if(loginLog == null){
            renderJBoltApiFail(JBoltMsg.DATA_NOT_EXIST);
            return;
        }
        renderJBoltApiSuccessWithData(loginLog);
    }

    /**
     * 状态选项
     */
    @JBoltHttpGet
    @Before(JBoltNoUrlPara.class)
    public void stateOptions() {
        renderJBoltApiSuccessWithData(JBoltEnum.getEnumOptionList(JBoltUserOnlineState.class));
    }
}
